package com.github.ginirohikocha.dbm.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.ginirohikocha.dbm.R;
import com.github.ginirohikocha.dbm.activity.OffersActivity;
import com.github.ginirohikocha.dbm.entity.Offer;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class RecycleCartAdapter extends RecyclerView.Adapter<RecycleCartAdapter.OfferHolder> {

    private OffersActivity activity;

    private List<Offer> offers = new ArrayList<>();

    public RecycleCartAdapter(Context context, List<Offer> offers) {
        this.activity = (OffersActivity) context;
        if (offers != null)
            this.offers = offers;
    }

    @NonNull
    @Override
    public OfferHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_offer, viewGroup, false);
        return new RecycleCartAdapter.OfferHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OfferHolder offerHolder, int i) {
        offerHolder.offer = offers.get(i);
        offerHolder.offerTitle.setText(offerHolder.offer.getOfferTitle());
        offerHolder.offerAmount.setText(String.format(Locale.CHINESE, "×%d", offerHolder.offer.getOfferAmount()));
        offerHolder.offerPrice.setText(String.format(Locale.CHINESE, "¥%.2f", offerHolder.offer.getOfferPrice().doubleValue()));
    }

    @Override
    public int getItemCount() {
        if (offers == null)
            return 0;
        return offers.size();
    }

//    //获取总价
//    public double getPriceCount() {
//        double prcie = 0;
//        for (Offer offer : offers) {
//            prcie += offer.getOfferPrice().doubleValue();
//        }
//        return prcie;
//    }

    public void refresh() {
        notifyDataSetChanged();
    }

    /**
     * 业务单元内部类
     **/
    class OfferHolder extends RecyclerView.ViewHolder {

        private Offer offer;

        private LinearLayout offerContent;
        private ImageView offerImg;
        private TextView offerTitle, offerAmount, offerPrice, offerStatus;

        public OfferHolder(@NonNull View itemView) {
            super(itemView);
            offerContent = itemView.findViewById(R.id.item_offer);
            offerImg = itemView.findViewById(R.id.item_offer_img);
            offerTitle = itemView.findViewById(R.id.item_offer_title);
            offerAmount = itemView.findViewById(R.id.item_offer_amount);
            offerPrice = itemView.findViewById(R.id.item_offer_price);
            offerStatus = itemView.findViewById(R.id.item_offer_status);
            offerStatus.setVisibility(View.GONE);
        }
    }
}
